<?php

use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $model backend\models\AdminUser */
/* @var $wxConfig */
$this->title = '特尊医疗';
?>
<section class="app-container app-form app-vip">
    <?= $this->render('flash') ?>
    <!-- form -->
    <?php
    if( $type != 1 ){
        ?>
        <div class="app-form-group">
            <label for="">备注信息：</label>
            <input type="text" id="remark" class="app-form-control" placeholder="请输入备注信息">
        </div>
        <?php
    }
    ?>

    <div class="app-form-upload">
        <label for="">请上传体检证明</label>

        <ul>
            <li class="add" id="bl_btn">
                <input type="button" id="bl_img">
            </li>
        </ul>
    </div>

    <!--            <div class="app-form-tips">-->
    <!--                <a href="javascript:void(0);">点击查看材料清单</a>-->
    <!--            </div>-->

    <div class="app-form-button">
        <button class="app-form-control app-btn-submit app-btn-small">提交</button>
    </div>

</section>

<section class="app-mask"></section>

<script src="/js/jquery.min.js"></script>
<script src="/js/hotcss.js"></script>
<script src="/js/fastclick.js"></script>
<script src="/js/swiper.min.js"></script>
<script src="/js/layer_mobile/layer.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js" type="text/javascript" charset="utf-8"></script>

<script>
    
    // factclick
    FastClick.attach(document.body);
    
    var banner = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        paginationClickable: true,
        loop: true,
        centeredSlides: true
    });
    var ids = [];
    
    $(function (){
        
        // 删除图片
        $('.app-form-upload').on('click', '.app-icon-close', function (){
            $(this).parent('li').fadeOut(function (){
                $(this).remove();
            })
        })
        
        $('.app-form-tips').on('click', function (){
            $('.app-mask').fadeIn();
        })
        
        $('.app-btn-submit').click(function (){
            layer.open({type: 2, shadeClose: false});
            var remark = $('#remark').val();
            $.post("<?= Url::toRoute(['member/save-shenqing']) ?>", {
                type: <?= $type ?>,
                ids: ids,
                remark: remark,
                "_csrf-frontend": "<?= Yii::$app->request->csrfToken ?>"
            }, function ( result ){
                layer.closeAll();
                if ( result.success == true ){
                    layer.open({
                        shadeClose: false,
                        content: '申请成功，请等待审核',
                        btn: '确定',
                        end: function (){
                            window.location.href = "<?= Url::toRoute(['site/index']) ?>"
                        }
                    });
                } else{
                    layer.open({
                        shadeClose: false,
                        content: result.msg,
                        btn: '确定'
                    });
                }
            }, 'json');
        });
        <?php
        if( $wxConfig ){
            
            echo 'wx.config(' . $wxConfig . ');';
        }
        ?>
        wx.ready(function (){
            document.querySelector('#bl_img').onclick = function (){
                var images = {
                    localId: [],
                    serverId: []
                };
                
                function upload( i, length, serverId ){
                    wx.uploadImage({
                        localId: images.localId[i],
                        success: function ( res ){
                            i++;
                            serverId.push(res.serverId);
                            if ( i < length ){
                                upload(i, length, serverId);
                            }
                            if ( i >= length ){
                                layer.open({type: 2, shadeClose: false});
                                $.ajax({
                                    dataType: 'json',
                                    data: {
                                        serverIds: '"' + serverId + '"',
                                        "_csrf-frontend": "<?= Yii::$app->request->csrfToken ?>"
                                    },
                                    type: 'POST',
                                    url: "<?= Url::toRoute(['main/upload-img']) ?>",
                                    success: function ( data ){
                                        if ( data.success == true ){
                                            var server_img = data.data.server_img;
                                            var server_str = '';
                                            for ( var i = 0; i < server_img.length; i++ ){
                                                $('#bl_btn').before('                    <li class="imaegs">\n' +
                                                    '                        <img src="' + server_img[i]['path'] + '" alt="">\n' +
                                                    '                        <span class="app-icon-close"></span>\n' +
                                                    '                    </li>');
                                                layer.closeAll();
                                                ids.push(server_img[i]['id']);
                                            }
                                        } else{
                                            alert(data.msg);
                                            layer.closeAll();
                                        }
                                    },
                                    error: function ( res ){
                                        alert(res.responseText);
                                    }
                                })
                            }
                        },
                        fail: function ( res ){
                            alert(JSON.stringify(res));
                        }
                    });
                }
                
                wx.chooseImage({
                    count: 9,
                    success: function ( res ){
                        images.localId = res.localIds;
                        var i = 0, length = images.localId.length;
                        serverId = [];
                        setTimeout(function (){
                            upload(i, length, serverId)
                        }, 100)
                    }
                });
            };
            /*
            document.querySelector('.item').onclick = function () {
                wx.previewImage({
                    urls: [
                    <volist name="photo" id="vo">
                    'http://'+window.location.host+'{$vo.path}',
                </volist>
                ]
            });
            };
           */
        });
        
    })


</script>